import java.util.ArrayList;

import javax.media.opengl.GL2;

import edu.umd.fosite.geom.Surface;


public class Building {
	Box base;
	ArrayList<Box> structure = new ArrayList<Box>();
	int maxH;
	public Building(Point origin, int x, int y, int z, Surface s)
	{
		maxH = y;
		int minY = Integer.MAX_VALUE;
		for(int a = (int) origin.x ; a < (int)(origin.x + x) ; a ++)
			for(int b = (int) origin.z ; b < (int)(origin.z + z) ; b ++)
			{
				minY = (int) Math.min(minY, s.at(a, b));
			}
		base = new Box(origin, x, minY-origin.y, z);
		build();
	}
	
	public void build()
	{
		structure.add(base);
		Box body = getRandomBox(base, maxH, .6, .2, .6, .8, .8);
		structure.add(body);
		
		int sideSections = (int)(Math.random()*4);
		int topSections = (int)(Math.random()*2)+1;
		
	}
	public void render(GL2 gl)
	{
		for(Box a : structure)
			a.render(gl);
		
	}
	public Box getRandomBox(Box bound, double maxY, double xP, double yP, double zP, double xAP, double zAP)
	{

		int bX = (int)(bound.dim[0]*(xP+Math.random()*(.9-xP)));
		int bY = (int)(maxY*(yP+Math.random()*(.9-yP)));
		int bZ = (int)(bound.dim[2]*(zP+Math.random()*(.9-zP)));
		int diffX = (int) (bound.dim[0]-bX);
		int diffZ = (int) (bound.dim[2]-bZ);
		int boX = (int) (diffX*(Math.random()*xAP + .9-xAP));
		int boZ = (int) (diffZ*(Math.random()*zAP + .9-zAP));
		return new Box(new Point(boX+bound.o.x, bound.o.y, boZ+bound.o.z), bX, bY, bZ);
	}
	
	public Box getRandomBox(Box bound, double xP, double yP, double zP, double xAP, double zAP)
	{

		int bX = (int)(bound.dim[0]*(xP+Math.random()*(.9-xP)));
		int bY = (int)(bound.dim[1]*(yP+Math.random()*(.9-yP)));
		int bZ = (int)(bound.dim[2]*(zP+Math.random()*(.9-zP)));
		int diffX = (int) (bound.dim[0]-bX);
		int diffZ = (int) (bound.dim[2]-bZ);
		int boX = (int) (diffX*(Math.random()*xAP + .9-xAP));
		int boZ = (int) (diffZ*(Math.random()*zAP + .9-zAP));
		return new Box(new Point(boX+bound.o.x, bound.o.y, boZ+bound.o.z), bX, bY, bZ);
	}
}
